//% color="#33CCFF" iconWidth=50 iconHeight=40
namespace DJIAiEducationKit {

      //% block="The AI camera initializes the soft serial port RX[RX]TX[TX]baud rate115200" blockType="command"
      //% RX.shadow="dropdown" TX.shadow="dropdown" TX.options="PIN_TX" RX.options="PIN_RX"   
      export function AI_init(parameter: any, block: any) {
        let rx = parameter.RX.code
        let tx = parameter.TX.code
        Generator.addInclude('open_protocol_tiny', '#include <open_protocol_tiny.h>');
        Generator.addDefine('READ_LENTH','50')
        Generator.addObject('sttag', 'typedef struct',`tag {
          uint8_t id;
          uint16_t x;
          uint8_t y;
          uint16_t w;
          uint8_t h;
          uint8_t c;
          } results_tag;`);
        Generator.addObject('open_protocol_sniffer_t1','open_protocol_sniffer_t',`uart_sniffer;`);
        Generator.addObject('open_protocol_sniffer_t2','open_protocol_sniffer_t',`usb_sniffer;`);
        Generator.addObject('uint8_t1','uint8_t',`buffer[50];`);
        Generator.addObject('uint16_t1','uint16_t',`little2big(uint8_t first_num, uint8_t second_num) {
            uint16_t result = 0;
            result = (second_num << 8) + first_num;
            return result;
          }`);
        Generator.addSetup(`Serial1`,`Serial1.begin(115200, ${rx}, ${tx});`)
        Generator.addSetup(`pen_protocol_init`,`open_protocol_init();`)
        Generator.addSetup(`open_protocol_sniffer_init`,`open_protocol_sniffer_init(&uart_sniffer);`)
      }
    //% block="Is serial port 1 data readable?" blockType="boolean"
    export function AI_getUart(parameter: any, block: any) {
      Generator.addCode(`Serial1.available()`)
    }
    //% block="Data parsing" blockType="command"
    export function AI_Unpack(parameter: any, block: any) {
      Generator.addCode(`         
      size_t realy_number;
      realy_number = Serial1.readBytes(buffer, READ_LENTH);
      open_protocol_sniffer(buffer, realy_number, &uart_sniffer, 0);
      open_protocol_pack_node_t *node = open_protocol_get_pack_node(&uart_sniffer);`)
    }
    //% block="Resolution available?" blockType="boolean"
    export function AI_Dataisavailable(parameter: any, block: any) {
      Generator.addCode(`node != NULL && node->header.cmd_id == 0x0210`)
    }
    //% block="Release the parsing data" blockType="command"
    export function AI_Releasedata(parameter: any, block: any) {
      Generator.addCode(`
      open_protocol_free_pack_node(node);
      while(Serial1.available()){
        Serial1.read();
      }
      
      `)
    }
  //% block="Get AI camera recognition number[MB]The ID of the target" blockType="reporter"
  //% MB.shadow="range"   MB.params.min=1    MB.params.max=21    MB.defl=1
  export function AI_ID(parameter: any, block: any) {
    let mb = parameter.MB.code
    Generator.addCode(`node->header.data[0 + ${mb-1} * 8]+1`)
  }
  //% block="Get AI camera recognition number[MB]The x-coordinate of the target" blockType="reporter"
  //% MB.shadow="range"   MB.params.min=1    MB.params.max=21    MB.defl=1
  export function AI_x(parameter: any, block: any) {
    let mb = parameter.MB.code
    Generator.addCode(`little2big(node->header.data[1 + ${mb-1} * 8],node->header.data[2 + ${mb-1} * 8])`)
  }
  //% block="Get AI camera recognition number[MB]The y-coordinate of the target" blockType="reporter"
  //% MB.shadow="range"   MB.params.min=1    MB.params.max=21    MB.defl=1
  export function AI_y(parameter: any, block: any) {
    let mb = parameter.MB.code
    Generator.addCode(`node->header.data[3 + ${mb-1} * 8]`)
  }
  //% block="Get AI camera recognition number[MB]The pixel width of the target" blockType="reporter"
  //% MB.shadow="range"   MB.params.min=1    MB.params.max=21    MB.defl=1
  export function AI_w(parameter: any, block: any) {
    let mb = parameter.MB.code
    Generator.addCode(`little2big(node->header.data[4 + ${mb-1} * 8], node->header.data[5 + ${mb-1} * 8])`)
  }
  //% block="Get AI camera recognition number[MB]The pixel height of the target" blockType="reporter"
  //% MB.shadow="range"   MB.params.min=1    MB.params.max=21    MB.defl=1
  export function AI_h(parameter: any, block: any) {
    let mb = parameter.MB.code
    Generator.addCode(`node->header.data[6 + ${mb-1} * 8]`)
  }
  //% block="Get AI camera recognition number[MB]Confidence level of the target, range: 0~100" blockType="reporter"
  //% MB.shadow="range"   MB.params.min=1    MB.params.max=21    MB.defl=1
  export function AI_c(parameter: any, block: any) {
    let mb = parameter.MB.code
    Generator.addCode(`node->header.data[8 + ${mb-1} * 8]`)
  }
}